package user;

import book.Library;
import constant.Constant;
import utils.PermissionException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/7/18
 * Time: 02:32
 */
public class ProxyUser {
    public User realUser;
    public ProxyUser(User realUser) {
        this.realUser = realUser;
    }
    //操作菜单
    public int menu(){
        return this.realUser.menu();
    }



    //管理员权限检查
    private void checkRealUserWhetherAdminUser(String message){
        try {
            if (!(this.realUser instanceof Admin)){
                throw new PermissionException(message);
            }
        }catch (PermissionException e){
            e.printStackTrace();
        }
    }
    //上架图书
    public void addBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限添加图书...");
        ((Admin)(this.realUser)).addBook();
    }
    //图书修改支持修改书名作者类别
    public void updateBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限更新图书...");
        ((Admin)(this.realUser)).updateBook();
    }
    //删除书籍
    public void removeBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限删除图书...");
        ((Admin)(this.realUser)).removeBook();
    }
    //统计每本书的借阅次数
    public void borrowCount() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看借阅次数...");
        ((Admin)(this.realUser)).borrowCount();
    }
    //查询最受欢迎的前K本书
    public void generateBook() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前K本书...");
        ((Admin)(this.realUser)).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态...");
        ((Admin)(this.realUser)).checkInventoryStatus();
    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        checkRealUserWhetherAdminUser("普通用户没有权限移除上架超过1年的书籍...");
        ((Admin)(this.realUser)).checkAndRemoveOldBooks();
    }


    //普通用户权限检查
    private void checkRealUserWhetherNormalUser(String message){
        try {
            if (!(this.realUser instanceof Normal)){
                throw new PermissionException(message);
            }
        }catch (PermissionException e){
            e.printStackTrace();
        }
    }
    //借阅图书
    public void borrowBook() {
        checkRealUserWhetherNormalUser("管理员没有权限借阅图书...");
        ((Normal)(this.realUser)).borrowBook();
    }
    //归还图书
    public void returnBook() {
        checkRealUserWhetherNormalUser("管理员没有权限归还图书...");
        ((Normal)(this.realUser)).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowBooks() {
        checkRealUserWhetherNormalUser("管理员没有权限查看个⼈借阅情况...");
        ((Normal)(this.realUser)).viewBorrowBooks();
    }

    Library library = Library.getLibrary();
    public void handleOperation(int choice) {
        if (this.realUser instanceof Admin) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CATEGORIZE_BOOK_BY_CATEGORY:
                    checkAndRemoveOldBooks();
                    break;
                default:
                    System.out.println("无效的操作");
                    break;
            }
        }else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowBooks();
                    break;
                default:
                    System.out.println("无效的操作");
                    break;
            }
        }
    }
}
